﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SGRapidForm.Common;
using SGRapidForm.Permission.Entity;
using FishWebLib;
using System.Collections.Specialized;
using System.Data;

namespace SGRapidForm.Permission.Component
{
    public class HelpComponent : ComponentBase
    {
        /// <summary>
        ///  根据过滤条件得到帮助页面
        /// </summary>
        /// <param name="filterConditions"></param>
        /// <returns></returns>
        public List<Help> Help_FilteredGetAll(string filterConditions)
        {
            NameValueCollection fieldCollection = SqlHelper.ParseQueryString(filterConditions);

            StringBuilder sql = new StringBuilder();
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "Title", DbType.String, " AND Title like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "HelpCode", DbType.String, " AND HelpCode like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "HelpCategoryKeyid", DbType.String, " AND HelpCategoryKeyid = '{0}' "));
            filterConditions = sql.ToString();
            return FishBLLHelper.CallSpGetDataItemList<Help>("sgrf_permission_Help_FilteredGetAll", null, filterConditions);
        }

        /// <summary>
        /// 新增帮助
        /// </summary>
        /// <param name="dept"></param>
        /// <returns></returns>
        public bool Help_Add(Help help)
        {
            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_permission_Help_Add", help) > 0);
        }

        /// <summary>
        /// 批量删除用户
        /// </summary>
        /// <param name="keyids">keyids</param>
        /// <returns>批量删除是否成功</returns>
        public bool Help_BulkDelete(string keyids)
        {
            //Keyid数量
            int count = SqlHelper.GetKeyidsCount(keyids);
            //Keyids转换成Sql语句
            keyids = SqlHelper.ConvertKeyidsToSqlStatement(keyids);

            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_permission_Help_BulkDelete", null, keyids) == count);
        }

        /// <summary>
        /// 根据Keyid获取一条帮助
        /// </summary>
        /// <param name="keyid">帮助的Keyid</param>
        /// <returns>帮助集合</returns>
        public Help Help_GetByKeyid(string keyid)
        {
            return FishBLLHelper.CallSpGetDataItem<Help>("sgrf_permission_Help_GetByKeyid", null, keyid);
        }

        /// <summary>
        /// 更新一条帮助
        /// </summary>
        /// <param name="UserDetails">一条帮助</param>
        /// <returns>是否更新成功</returns>
        public bool Help_Update(Help help)
        {
            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_permission_Help_Update", help) > 0);
        }

        /// <summary>
        /// 删除帮助 
        /// </summary>
        /// <param name="keyid">keyid</param>
        /// <returns>是否更新成功</returns>
        private static string tableName = "sgrf_permission_Help";

        public bool Help_Delete(string keyid)
        {
            return (FishBLLHelper.CallSpExecuteNonQuery("sgrf_common_Delete", null, keyid, tableName) > 0);
        }

        /// <summary>
        /// 进行过滤、排序、分页查询用户
        /// </summary>
        /// <param name="startRecord">开始记录索引</param>
        /// <param name="maxRecords">每页记录数</param>
        /// <param name="sortColumns">排序列</param>
        /// <param name="filterConditions">过滤条件SQL</param>
        /// <returns>用户集合</returns>
        public List<Help> Help_PagingGetHelp(Int32 startRecord, Int32 maxRecords, String sortColumns, String filterConditions)
        {
            VerifySortColumns(sortColumns);
            if (string.IsNullOrWhiteSpace(sortColumns))
            {
                sortColumns = " sgrf_permission_Help.[Order] ASC";
            }
            return FishBLLHelper.CallSpGetDataItemList<Help>("sgrf_permission_Help_PagingGetHelp", null, startRecord, maxRecords, sortColumns, ProcessFilterConditions(filterConditions));
        }

        /// <summary>
        /// 验证排序参数是否包含非法字符，有则抛出异常
        /// </summary>
        /// <param name="sortColumns"></param>
        private void VerifySortColumns(string sortColumns)
        {
            sortColumns = sortColumns.ToLowerInvariant().Replace(" asc", String.Empty).Replace(" desc", String.Empty);
            String[] columnNames = sortColumns.Split(',');

            foreach (String columnName in columnNames)
            {

                switch (columnName.Trim().ToLowerInvariant())
                {
                    case "title":
                    case "helpcode":
                    case "":
                        break;
                    default:
                        throw new ArgumentException("SortColumns contains an invalid column name.");
                }
            }
        }

        /// <summary>
        /// 进行过滤、排序、分页查询用户记录数
        /// </summary>
        /// <param name="filterConditions">过滤条件</param>
        /// <returns>记录数</returns>
        public int Help_PagingGetHelpCount(String filterConditions)
        {

            return Convert.ToInt32(FishBLLHelper.CallSpExecuteScalar("sgrf_permission_Help_PagingGetHelpCount", null, ProcessFilterConditions(filterConditions)));
        }

        /// <summary>
        /// 处理拼接过滤Sql语句
        /// </summary>
        /// <param name="filterConditions"></param>
        /// <returns></returns>
        private string ProcessFilterConditions(string filterConditions)
        {
            NameValueCollection fieldCollection = SqlHelper.ParseQueryString(filterConditions);
            StringBuilder sql = new StringBuilder();
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "1", DbType.Int32, " AND 1 ={0} "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "HelpCode", DbType.String, "AND HelpCode like '%{0}%'"));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "Title", DbType.String, " AND Title like '%{0}%' "));
            sql.Append(SqlHelper.GenerateSqlStatement(fieldCollection, "HelpCategoryKeyid", DbType.String, " AND HelpCategoryKeyid = '{0}' "));
           

            return sql.ToString();
        }
    }
}
